/**
 * 
 */
package vn.tecviets.thuandt.model.bo;

import java.sql.SQLException;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;

import vn.tecviets.thuandt.model.common.Constant;
import vn.tecviets.thuandt.model.dao.UserDAO;

/*
 * LoginBO.java
 * 1.0
 * Mar 19, 2013 1:27:22 PM
 * 
 * Copyright notice
 *
 * Modification Logs:
 *
 * DATE					AUTHOR				DESCRIPTION
 * -----------------------------------------------------------
 * Mar 19, 2013			ThuanDT				Creation
 */

public class LoginBO {
	UserDAO userDAO = new UserDAO();

	/**
	 * Dùng để kiểm tra thông tin của user
	 * 
	 * @param username
	 * @param password
	 * @return Nếu userID và password đúng thì trả về một chuỗi thể hiện role
	 *         của user. Nếu userID hoặc password sai thì trả về null.
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public String getUserRole(String username, String password) throws ClassNotFoundException, SQLException {
		String role = null;

		try {
			role = userDAO.selectUserRole(username, password);
		} catch (ClassNotFoundException e) {
			throw e;
		} catch (SQLException e) {
			throw e;
		}

		return role;
	}

	/**
	 * @param username
	 * @param password
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public ActionErrors validate(String username, String password, String role) {
		
		ActionErrors actionErrors = new ActionErrors();

		if ("".equals(username)) {
			actionErrors.add("usernameMessageError", new ActionMessage("error.emptyUsername"));
		} else if (username.length() > Constant.USERNAME_MAX_LENGTH) {
			actionErrors.add("usernameMessageError", new ActionMessage("error.longUsername", Constant.USERNAME_MAX_LENGTH));
		}

		if ("".equals(password)) {
			actionErrors.add("passwordMessageError", new ActionMessage("error.emptyPassword"));
		}

		if (!"".equals(username) && !"".equals(password) && role == null) {
			actionErrors.add("loginMessageError", new ActionMessage("error.loginInvalidInfo"));
		}

		return actionErrors;
	}

}
